<template>
	<view>
		<scroll-view @scrolltolower="atBottom" scroll-y style="height: calc(100vh - 110px);">
			<template v-for="(item, index) in listData">
				<view class="data-list" @click="toInfo(item)" :key="index">
					<view class="">
						<checkbox :checked="item.checked" style="transform:scale(0.8)" @click.stop="checkItem(item)"/>
					</view>
					<view class="data-item">
						<view class="item-text item-more">
							<view class="item-level">
								<view class="level">{{item.dfLevelNo | codeFormat(levelOptions)}}</view>
							</view>
							<view class="item-title">
								{{item.manageObjName}}/{{item.trueLocaName}}
							</view>
							<view class="item-states">
								{{item.state | codeFormat(stateOptions)}}
							</view>
						</view>
						<view class="item-text">
							<view class="item-desc">{{item.dfDesp}}</view>
						</view>
						<view class="item-text item-desc item-more">
							<view>{{item.dfType | codeFormat(typeOptions)}}</view>
							<view>{{item.skillNo | codeFormat(majorOptions)}}</view>
							<view>{{item.finddate | dateFormat('yyyy-MM-dd HH:mm')}}</view>
						</view>
					</view>
				</view>
			</template>
			<view class="textcenter" v-if="isEnd">
				{{$t('baseCommon.message.noMoreData')}}
			</view>
		</scroll-view>
		
		<view class="list-btn">
			<button class="fixed-btn" type="primary" @click="delDefect">{{$t('defect.buttons.deleteDefect')}}</button>
		</view>
	</view>
	
</template>

<script>
	import { defect as constants } from '@/utils/business/constants.js'
	import * as sqliteUtil from '@/utils/sqlite.js'
	import { notice, utilities } from '@/utils/common.js'
	
	export default {
		data() {
			return {
				listData: [],
				isEnd: false,
				stateOptions: constants.dfStateOptions,
				levelOptions: [],
				typeOptions: constants.dfTypeOptions,
				majorOptions: [],
				checkedList: []
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			async init() {
				await sqliteUtil.selectTableInfo('m_df_level').then(res=>{
					this.levelOptions = []
					// 下划线转驼峰
					utilities.formatHumpLineTransfer(res)
					res.forEach(item=>{
						this.levelOptions.push({
							value: item.dfLevelNo,
							label: item.dfLevel
						})
					})
				})
				
				// 专业
				await sqliteUtil.selectTableInfo('m_cpis_cat_dic', 'type_key', 'zybm').then(res=>{
					this.majorOptions = []
					res.forEach(item=>{
						this.majorOptions.push({
							value: item.key,
							label: item.name
						})
					})
				})
				
				this.atTop()
			},
			atBottom() {
				if (!this.isEnd) {
					this.loadData()
				}
			},
			atTop() {
				this.isEnd = false;
				this.listData = [];
				this.loadData();
			},
			loadData() {
				uni.showLoading({
					title: this.$t('common.loading')
				})
				sqliteUtil.selectTableInfo('m_df_information').then(res=>{
					uni.hideLoading()
					
					if (!res) {
						return
					}
					if (res && res.length > 0) {
						// 下划线转驼峰
						utilities.formatHumpLineTransfer(res)
						for(let data of res) {
							data.trueLocaName = data.locaName.substr(data.locaName.lastIndexOf('/')+1, data.locaName.length)
						}
						this.listData = this.listData.concat(res);
						this.isEnd = true;
					} else {
						this.isEnd = true;
					}
				})
			},
			toInfo(item) {
				uni.navigateTo({
					url: "/pages/business/defectfire/offline/detail?dfId="+item.dfId+"&type=edit"
				})
			},
			checkItem(item) {
				item.checked = !item.checked
			},
			getCheckedList() {
				this.checkedList = []
				this.listData.forEach(item=>{
					if(item.checked) {
						this.checkedList.push(item)
					}
				})
			},
			closePop() {
				this.$refs.popup.close()
				this.atTop()
			},
			delDefect() {
				this.getCheckedList()
				if(this.checkedList.length === 0) {
					notice.toast(this.$t('baseCommon.message.pleaseSelect', { title: this.$t('defect.common.def') }))
					return
				}
				if(this.checkedList.length > 1) {
					notice.toast(this.$t('defect.message.onlyOneDefect'))
					return
				}
				
				this.checkedList.forEach(async task=>{
					await this.deleteOfflineDefect(task.dfId)
				})
				this.atTop()
			},
			async deleteOfflineDefect(dfId) {
				// 删除 m_df_information表数据
				await sqliteUtil.deleteTableInfo('m_df_information', 'df_id', dfId)
				// 清空巡检检查项关联的缺陷字段
				let inspectList = await sqliteUtil.selectTableInfo('m_ri_task_inspection')
				inspectList.forEach(item=>{
					if(item.df_no == dfId) {
						let data = [
							{
								key: "df_no",
								value: ""
							}
						]
						let condition = [
							{
								key: "task_insp_id",
								value: item.task_insp_id
							}
						]
						sqliteUtil.modifyTableInfo('m_ri_task_inspection', data, condition)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '@/static/style/common-list.scss';
	
	.data-list {
		padding: 8px;
	}
	
	.item-level {
		margin-right: 8px;
		
		.level {
			width: 40px;
			height: 40px;
			color: #FFFFFF;
			text-align: center;
			line-height: 40px;
			font-size: 12px;
			background: url(../../../../../static/images/icon/8.png);
			background-size: 100% 100%;
		}
	}
	
	.item-states {
		margin-left: 5px;
		min-width: 60px;
		color: $uni-color-primary;
		text-align: right;
	}
	
	.item-title {
		min-width: calc(100% - 115px);
	}
	
	.item-more {
		align-items: center;
		justify-content: space-between;
	}
	
	.textcenter {
		font-size: 14px;
		padding: 8px 0;
		color: #333;
		text-align: center;
	}
</style>
